package algorithms.leecode;

public class Au_38 {
    public static void main(String[] args) {
        System.err.println((int)'.');
    }
    public static String countAndSay(int n) {
        return df("11", n, 1);
    }

    public static String df(String s, int n, int index) {
        if (n == 1) {
            s = "1";
            return s;
        }
        if (n == 2) {
            s = "11";
            return s;

        }
        if (index == n - 1) {
            return s;
        }
        index++;
        char[] carr = s.toCharArray();
        StringBuilder builder = new StringBuilder();
        int count = 1;
        boolean lastSame;
        // 如果当前和后面数字相同则进入计数，否则写入
        for (int i = 0; i < carr.length - 1; i++) {
            if (carr[i] == carr[i + 1]) {
                // 如果倒数第二个和最后一个相同
                if (i == carr.length - 2) {
                    builder.append(++count).append(carr[i]);
                }
                count++;
            } else {
                builder.append(count).append(carr[i]);
                // 如果倒数第二个和最后一个不同
                if (i == carr.length - 2) {
                    builder.append(1).append(carr[i + 1]);
                }
                count = 1;
            }
        }
        return  df( builder.toString(),  n,  index);

    }
}
